$(function(){
	var firstLevelLink = [];
	var secondLeveLink = [];
	var thirdLevelLink = [];
	var forthLevelLink = [];
	var firstLinkID = "";
	var clickFlag = -1;
	
//	QuickLink
	var quickLink_left = [];
	var quickLink_right = [];
	var quickLink_right1 = [];
	var quickLink_right2 = [];
	
	
	$(document).ready(function(){ 

		$.ajax({
			type : "POST",
			url : "searchJsonLink.action",
			dataType : "json",
			success : function(link) {
				$.each(link, function(index, value) {
					if (value.catalogueLevel === 1) {
						firstLevelLink.push(value);
					}
				});

//				便利firstLevelLink里的每一个object
				$.each(firstLevelLink, function(index, value) {
					
//		                        对于firstLevelLink里的每一个object			
					
//					添加相应的二级标题数组
					generatorSecondLink(value, link);

					
//					添加相应二级标题下的三级标题数组
//					$.each(secondLeveLink, function(index1, value1) {
//
//						generatorThirdLink(secondLeveLink[index1], link);
//					});
//
//					$.each(thirdLevelLink, function(index2, value2) {
//
//						generatorForthLink(thirdLevelLink[index2], link);
//					});
					
				     	generatorThirdLink(secondLeveLink[index], link);
	
						generatorForthLink(thirdLevelLink[index], link);

				});

				console.info("firstLevelLink:");
				console.info(firstLevelLink);
				console.info("secondLeveLink:");
				console.info(secondLeveLink);
				console.info("thirdLevelLink:");
				console.info(thirdLevelLink);
				console.info("forthLevelLink:");
				console.info(forthLevelLink);

				 var links = " ";
					
				 $.each(firstLevelLink,function(key, value) {
		      
					 var towID = key + 1;
					 links += "<li class='linkslist'><p class='arryId'>"+key+"</p><a class='navs-links' class='activeTab' id = 'tow"+ towID +"' onclick='setTab('tow',"+ towID +",7)'>" + value.linkName +"</a></li>";
	
		             });
		           $('.links-title').append(links); 
		           $('.arryId').hide();
			}
		});
		
		
//	QuickLink Array ready	
		$.ajax({
			type : "POST",
			url : "searchQuickLink.action",
			dataType : "json",
			success : function(quickLinkList) {
				$.each(quickLinkList, function(index, value) {

					if (value.catalogueParentID === 1000) {
						quickLink_left.push(value);
					} else {
						quickLink_right.push(value);
					}

				});

				$.each(quickLink_right, function(index1, value1) {
					if (value1.catalogueLevel === 1) {
						quickLink_right1.push(value1);
					}
		
				});
				
				generatorRightQuickLink1(quickLink_right1, quickLinkList);
				
				
				console.info("quickLink_left: @@@@@@@@@@@@@@@@@@@@");
				console.info(quickLink_left);
			   /* console.info("quickLink_right:");
				console.info(quickLink_right);*/
				console.info("quickLink_right1:");
				console.info(quickLink_right1);
				console.info("quickLink_right2:");
				console.info(quickLink_right2);

			}
		});
			
	});
	
	
	// topbar tab
    function setTab(name, m, n) {
           for (var i = 1; i <= n; i++) {
               var menu = document.getElementById(name + i);
               var showDiv = document.getElementById("cont_" + name + "_" + i);
                showDiv.className = i == m ? "activeTab" : "";
                showDiv.style.display = i == m ? "block" : "none";
           }
       }
	
	
	
//	点击一级标题的方法
    $("#links-title").on("click",".linkslist",function(){
    	
//   	 <div id="cont_tow_1" class = "activeTab" style="display:none;">
    	var totalStr = "";
    	
		 var secTitles = " ";
		
		 var thrTitles = " ";
		 
		firstLinkID = $(this).children(".arryId").text();
		
		
if(parseInt(clickFlag) == parseInt(firstLinkID)){
			
			$("#thisLocal").empty();
			clickFlag = -1;
			
		}else{
		
			clickFlag = firstLinkID;
		
		var towID = parseInt(firstLinkID)+ 1;
		
		console.info(typeof(firstLinkID));
		
		totalStr += "<div id='cont_tow_"+ towID +"' class = 'activeTab'>";
		
		
		
		if(firstLinkID == 0){
			totalStr += "<div class='content2'><ul>";
			
			var quickLinkStr = "";
			
//			left Quick Link
			 $.each(quickLink_left,function(key, value){
				 if(value != "" ){
					 totalStr +="<li><p class='arryId'>"+key+"</p><a style = 'color:white' href = '"+value.linkURL +"'>> "+ value.linkName +"</a></li>";
				 }
				 });
			 totalStr +="<ul>";
//	     	     取quick link 右边每一个一级标题
			 $.each(quickLink_right1,function(key1, value1){
				 if(value1 != ""){
					 totalStr +="<li><p class='arryId'>"+key1+"</p><a style = 'color:white'>"+ value1.linkName +"</a></li>";
					 }
				 totalStr +="<ul>";
				 
//				     取quick link 右边每一个一级标题下的四级标题
					 $.each(quickLink_right2[key1],function(key2, value2){
						 if(value2 != ""){
							 totalStr +="<li><p class='arryId'>"+key2+"</p><a style = 'color:white' href = '"+value2.linkURL +"'>> "+ value2.linkName +"</a></li>";
						 }
						 });

					 totalStr +="</ul>";
					 
			 });
			 totalStr += "</div>";
			 totalStr += "</div>";
			 
			/* console.info("quickLinkStr @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:");
			console.info(quickLinkStr);
			*/
		
			$("#thisLocal").empty();			
		
			 $("#thisLocal").append(totalStr); 
			 $('.arryId').hide();
			 
		}else{

		totalStr += "<div class='subTitle'><ul class='subNav'>"; 
		
		var firArray = new Array();
		 firArray = secondLeveLink[firstLinkID];
		
		var secArray = new Array();
		 secArray = thirdLevelLink[firstLinkID];
		
		var thiArray = new Array();
		 thiArray = forthLevelLink[firstLinkID];
		
//		 var firstLinkID = $(this).children.val();
		 
//		 如果選中的一級标题  不是  QuickLink 则铺该一级标题下的二级标题
		 
		 $.each(firArray,function(key, value) {

			 totalStr += "<li class='secLinkslist'><p class='arryId'>"+key+"</p><a>"+ value.linkName +"</a></li>";
		 });
		 
		 totalStr += "</ul></div><div class='content2'>";
		 
//		显示第0个二级标题下的三级标题
		    var tArr = new Array();
		     tArr = secArray[0];
		     
			 $.each(tArr,function(key1, value1){
				 if(value1 != ""){
					 if(value1.catalogueLevel == 4)
					 {    totalStr +="<ul>";
						 totalStr +="<li><p class='arryId'>"+key1+"</p><a style = 'color:white' href = '"+value1.linkURL +"'>> "+ value1.linkName +"</a></li>";
						 totalStr +="</ul>";
						 
					 }else{
					 
					 totalStr +="<li><p class='arryId'>"+key1+"</p><a style = 'color:white'>"+ value1.linkName +"</a></li>";
					 }
				 }
				 totalStr +="<ul>";
				
//				 $.each(thiArray,function(key2, value2){
//			   	每级三级标题下的四级标题
					 var thisArray = new Array();
						 thisArray = thiArray[0];
						
						 $.each(thisArray[key1],function(key3, value3){
							 if(value3 != ""){
								 totalStr +="<li><p class='arryId'>"+key3+"</p><a style = 'color:white' href = '"+value3.linkURL +"'>> "+ value3.linkName +"</a></li>";
							 }
							 });
//				 });
						 totalStr +="</ul>";
			 });
			 totalStr += "</div>";
			 totalStr += "</div>";
		
			 $("#thisLocal").empty();	
			   /* console.info("secLink @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:");
				console.info(secTitles);
			 
				 console.info("third and forth Link @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:");
				console.info(thrTitles);*/
				
			 console.info(totalStr);
			 
		 $("#thisLocal").append(totalStr); 
		
		 $('.arryId').hide();
		  
		}
		}
});
	
//    点击二级标题 ####################################################################################################################
    $("#thisLocal").on("click",".secLinkslist",function(){
//    function setTab2(key){
//	    	alert("HHHHHHHHHHH")

//    	var id=$(".arryId",$(this)).text();
//    	console.info(id);
    	
    	var totalStr = "";
    	
    	var towID = parseInt(firstLinkID)+ 1;
		totalStr += "<div id='cont_tow_"+ towID +"' class = 'activeTab'>";
    	
		totalStr += "<div class='subTitle'><ul class='subNav'>";
		
    	
    	if(firstLinkID != 0){
    			
    		//secLinkID = $(".arryId_2",$(this)).text();
    		secLinkID = $(this).children(".arryId").text();
    		
    		var secondArray = new Array();
    		secondArray = thirdLevelLink[firstLinkID];
    		
    		var thirdArray = new Array();
    		thirdArray = secondArray[secLinkID];
    		
    		var thrTitles = "";
    		
    	 	var firArray = new Array();
   	    	 firArray = secondLeveLink[firstLinkID];
    		
//    		 如果選中的一級标题  不是  QuickLink 则铺该一级标题下的二级标题
    		 
    		 $.each(firArray,function(key, value) {

    			 totalStr += "<li class='secLinkslist'><p class='arryId'>"+key+"</p><a style = 'color:white'>"+ value.linkName +"</a></li>";
    		 });
    		 
    		
    		 totalStr += "</ul></div><div class='content2'>";
    		 
    		
// 选中二级标题下的三级标题  		
   			 $.each(thirdArray,function(key1, value1){
   				 if(value1 != ""){
   					if(value1.catalogueLevel == 4)
					 {    totalStr +="<ul>";
						 totalStr +="<li><p class='arryId'>"+key1+"</p><a style = 'color:white' href = '"+value1.linkURL +"'>> "+ value1.linkName +"</a></li>";
						 totalStr +="</ul>";
						 
					 }else{
					 
					 totalStr +="<li><p class='arryId'>"+key1+"</p><a style = 'color:white'>"+ value1.linkName +"</a></li>";
					 }
   				 }
   				totalStr +="<ul>";
    		
//   	每级三级标题下的四级标题
    		  var secArr = new Array();
				 secArr = forthLevelLink[firstLinkID];
    		
			  var thiArr = new Array();
				 thiArr = secArr[secLinkID];
				 
				 
				 $.each(thiArr[key1],function(key3, value3){
					 if(value3 != ""){
						 totalStr +="<li><p class='arryId'>"+key3+"</p><a style = 'color:white' href = '"+value3.linkURL +"'>> "+ value3.linkName +"</a></li>";
					 }
					 });
				 totalStr +="</ul>";
   			});
   			 
    		 totalStr += "</div>";
    		 totalStr += "</div>";
   		
    		 console.info(totalStr);
    		 
			 $("#thisLocal").empty();	
   			 
			 $("#thisLocal").append(totalStr); 
   			 
   			  $('.arryId').hide();
    	}
    });
    
//   #####################################################################################################################################
    
    
    
	//处理二级标题方法
	function generatorSecondLink(fir, allLink) {
		var temp = new Array();
		//遍历所有link，根据ParentID 和 linkID 取出一级标题下的二级标题，
		//和直接放在一级标题下的四级标题放进secondLeveLink[]数组中
		$.each(allLink, function(index, value) {
			if (value.catalogueParentID == fir.linkID) {
				temp.push(value);
			}
		})
		secondLeveLink.push(temp);
	}

	//处理三级标题方法
	function generatorThirdLink(sec, allLink) {

		var temp2 = new Array();

		//遍历所有secondLeveLink，根据ParentID 和 linkID 取出二级标题下的三级标题以及直接放在二级标题下的四级标题，
		//将他们都放进thirdLevelLink[]数组中
		$.each(sec, function(index, value) {
			
			var temp1 = new Array();
			$.each(allLink, function(index1, value1) {
				
				if (value1.catalogueParentID == value.linkID) {
					temp1.push(value1);
				}
			});
				temp2.push(temp1);
		});
			thirdLevelLink.push(temp2);
	}
	

	//处理四级标题方法
	function generatorForthLink(thr, allLink) {

		//遍历所有thirdLevelLink，根据ParentID 和 linkID 取出所有三级标题下的四级标题，
		//将他们都放进forthLevelLink[]数组中
		//四级标题为最小级别标题，此级标题有对应的URL

		var temp3 = new Array();
		$.each(thr, function(index, value) {
			var temp4 = new Array();
			$.each(value, function(index1, value1) {
				//if (value1.catalogueParentID == value.linkID) {
				var temp5 = new Array();
				$.each(allLink, function(index2, value2) {

					if (value2.catalogueParentID == value1.linkID) {
						temp5.push(value2);
					}
				});
					temp4.push(temp5);
			});
				temp3.push(temp4);
		});
			forthLevelLink.push(temp3);
	}
	
	
	// 处理右边四级级标题方法  quickLink_right1, quickLinkList
	function generatorRightQuickLink1(right, allLink) {
		
		$.each(right, function(index1, value1) {
			var temp = new Array();

			$.each(allLink, function(index, value) {
				if (value.quickLink_Catalogue == value1.linkID) {
					temp.push(value);
				}
			})
			quickLink_right2.push(temp);
		})
		
	}

	
/*	//处理三级标题方法
	function generatorThirdLink(sec, allLink) {

		var temp2 = new Array();

		//遍历所有secondLeveLink，根据ParentID 和 linkID 取出二级标题下的三级标题以及直接放在二级标题下的四级标题，
		//将他们都放进thirdLevelLink[]数组中
		$.each(sec, function(index, value) {
			var temp1 = new Array();

			$.each(allLink, function(index1, value1) {

				if (value1.catalogueParentID == value.linkID) {

					temp1.push(value1);
				}
			});
			var len = temp1.length;
			if (len) {
				temp2.push(temp1);
			}
		});
		var len2 = temp2.length;
		if (len2) {
			thirdLevelLink.push(temp2);
		}

	}

	//处理四级标题方法
	function generatorForthLink(thr, allLink) {

		//遍历所有thirdLevelLink，根据ParentID 和 linkID 取出所有三级标题下的四级标题，
		//将他们都放进forthLevelLink[]数组中
		//四级标题为最小级别标题，此级标题有对应的URL

		var temp3 = new Array();
		$.each(thr, function(index, value) {
			var temp4 = new Array();
			$.each(value, function(index1, value1) {
				//if (value1.catalogueParentID == value.linkID) {
				var temp5 = new Array();
				$.each(allLink, function(index2, value2) {

					if (value2.catalogueParentID == value1.linkID) {
						temp5.push(value2);
					}
				});

				var len5 = temp5.length;
				if (len5) {
					temp4.push(temp5);
				}
			});

			if (temp4.length) {
				temp3.push(temp4);
			}
		});

		if (temp3.length) {
			forthLevelLink.push(temp3);
		}
	}*/
	
	
});